package com.dci.service.impl.project;

import com.dci.dao.pms.project.FeedbackInfoMapper;
import com.dci.dao.pms.project.PlanFeedbackInfoMapper;
import com.dci.dao.pms.project.PlanInfoMapper;
import com.dci.dao.pms.project.ProjectInfoMapper;
import com.dci.model.project.FeedbackInfo;
import com.dci.model.project.PlanFeedbackInfo;
import com.dci.model.project.PlanInfo;
import com.dci.model.project.ProjectInfo;
import com.dci.result.JsonData;
import com.dci.service.project.PlanFeedbackInfoService;
import com.dci.util.UUIDBuild;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.service.impl.project
 * @Description: 反馈信息实现类
 * @author: 张星亮
 * @date: 2019/2/25
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PlanFeedbackInfoServiceImpl implements PlanFeedbackInfoService {

    @Resource
    private PlanFeedbackInfoMapper planFeedbackInfoMapper;

    @Resource
    private PlanInfoMapper planInfoMapper;

    @Resource
    private ProjectInfoMapper projectInfoMapper;

    @Resource
    private FeedbackInfoMapper feedbackMapper;


    @Override
    public JsonData checkFeedbackStatus(PlanFeedbackInfo record) {
        PlanFeedbackInfo feedbackInfo = planFeedbackInfoMapper.selectByParas(record);
        if (feedbackInfo != null) {
            // 已经存在
            return JsonData.success(feedbackInfo, 906);
        } else {
            // 不存在
            return JsonData.success(null, 911);
        }
    }

    @Override
    public JsonData insert(PlanFeedbackInfo record) {
        PlanInfo planInfo = new PlanInfo();
        planInfo.setId(record.getPlanInfoId());
        planInfo = planInfoMapper.selectByPrimaryKey(planInfo);
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(date);
        // 判断 新增 or 更新
        if (record != null && record.getId() != null) {
            // 更新
            record.setStatus(0);
            record.setAuditOpinion(null);
            planFeedbackInfoMapper.updateByPrimaryKeySelective(record);
            record = planFeedbackInfoMapper.selectByPrimaryKey(record);
            String records = planInfo.getRecord() + time + ",【储备主体】 工作进度反馈：" + record.getFeedback() + " 进度申报：" + record.getCompleteProgress() + "%;";
            planInfo.setRecord(records);
            planInfoMapper.updateByPrimaryKeySelective(planInfo);
        } else {
            record.setId(UUIDBuild.getUUID());
            record.setStatus(0);
            record.setDelFlag("0");
            record.setAuditOpinion(null);
            planFeedbackInfoMapper.insert(record);
            String records = "";
            if (planInfo.getRecord() != null) {
                records = planInfo.getRecord() + time + ",【储备主体】 工作进度反馈：" + record.getFeedback() + " 进度申报：" + record.getCompleteProgress() + "%;";
            } else {
                records = time + ",【储备主体】 工作进度反馈：" + record.getFeedback() + " 进度申报：" + record.getCompleteProgress() + "%;";
            }
            planInfo.setRecord(records);
            planInfoMapper.updateByPrimaryKeySelective(planInfo);
        }
        String feedback = "【储备主体】 工作进度反馈：" + record.getFeedback() + " 进度申报：" + record.getCompleteProgress() + "%";
        FeedbackInfo feedbackInfo = FeedbackInfo.builder().plan_feedback_id(record.getId()).feedback_person("0").info(record.getFeedback()).feedback_id(record.getPlanInfoId()).file(record.getFeedback_file()).progess(record.getCompleteProgress()).url(record.getFeedback_url()).time(time).build();
        feedbackMapper.insert(feedbackInfo);
        return JsonData.success(record, 906);
    }

    /**
     * 审核
     *
     * @param record
     * @return
     */
    @Override
    public JsonData auditFeedback(PlanFeedbackInfo record) {
        // 审核通过状态
        planFeedbackInfoMapper.updateByPrimaryKeySelective(record);

        if (record.getStatus() == 1) {
            // 将完成情况同步到项目进度计划表里
            PlanInfo planInfo = new PlanInfo();
            planInfo.setId(record.getPlanInfoId());
            planInfo = planInfoMapper.selectByPrimaryKey(planInfo);
            planInfo.setProgress(record.getCompleteProgress());
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(date);
            if (record.getAuditOpinion() == null) {
                record.setAuditOpinion("无");
            }
            String records = planInfo.getRecord() + time + ",【储备组工作人员】 审核意见：" + record.getAuditOpinion() + "  操作：同意通过;";
            planInfo.setRecord(records);
            planInfoMapper.updateByPrimaryKeySelective(planInfo);
            List<FeedbackInfo> list1 = feedbackMapper.selectByKeyId(record.getId());
            if (list1.size() > 0) {
                for (FeedbackInfo info : list1) {
                    String feedback = "【储备组工作人员】 审核意见：" + record.getAuditOpinion() + "  操作：同意通过";
                    info.setAudit_info(record.getAuditOpinion());
                    info.setAudit_time(time);
                    info.setAudit_person("1");
                    info.setAudit_type("审核通过");
                    feedbackMapper.updateById(info);
                }
            }
            List<PlanInfo> list = planInfoMapper.selectByProgress(planInfo);
            double progress = 0;
            for (PlanInfo plan : list) {
                if (plan.getProgress() != null) {
                    int weight = Integer.valueOf(plan.getWeight());
                    int gress = plan.getProgress();
                    progress = (double) weight * (double) gress * 0.01 + progress;
                }
            }
            int progress_number = (int) progress;
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.setId(record.getProjectId());
            projectInfo.setProgress_number(progress_number);
            projectInfoMapper.updateByProjectId(projectInfo);
        } else {
            // 将完成情况同步到项目进度计划表里
            PlanInfo planInfo = new PlanInfo();
            planInfo.setId(record.getPlanInfoId());
            planInfo = planInfoMapper.selectByPrimaryKey(planInfo);
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String time = sdf.format(date);
            if (record.getAuditOpinion() == null) {
                record.setAuditOpinion("无");
            }
            String records = planInfo.getRecord() + time + ",【储备组工作人员】 审核意见：" + record.getAuditOpinion() + "  操作：驳回;";
            planInfo.setRecord(records);
            planInfoMapper.updateByPrimaryKeySelective(planInfo);
            List<PlanInfo> list = planInfoMapper.selectByProgress(planInfo);
            double progress = 0;
            for (PlanInfo plan : list) {
                if (plan.getProgress() != null) {
                    int weight = Integer.valueOf(plan.getWeight());
                    int gress = plan.getProgress();
                    progress = (double) weight * (double) gress * 0.01 + progress;
                }
            }
            int progress_number = (int) progress;
            ProjectInfo projectInfo = new ProjectInfo();
            projectInfo.setId(record.getProjectId());
            projectInfo.setProgress_number(progress_number);
            projectInfoMapper.updateByProjectId(projectInfo);
            List<FeedbackInfo> list1 = feedbackMapper.selectByKeyId(record.getId());
            if (list1.size() > 0) {
                for (FeedbackInfo info : list1) {
                    String feedback = "【储备组工作人员】 审核意见：" + record.getAuditOpinion() + "  操作：驳回";
                    info.setAudit_info(record.getAuditOpinion());
                    info.setAudit_time(time);
                    info.setAudit_person("1");
                    info.setAudit_type("驳回");
                    feedbackMapper.updateById(info);
                }
            }
            planFeedbackInfoMapper.deleteById(record);
        }
        return JsonData.success(record, 906);
    }


    @Override
    public PlanFeedbackInfo selectByPrimaryKey(PlanFeedbackInfo record) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(PlanFeedbackInfo record) {
        return 0;
    }


}
